package net.skyedu.service.administrator;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.skyedu.bean.Appeal;
import net.skyedu.dao.UserInfoDao;
import net.skyedu.utils.Page;
import net.skyedu.utils.PageHelp;

public class Accepts extends HttpServlet {

	private static final long serialVersionUID = 425046001125171317L;

	/**
	 * Constructor of the object.
	 */
	public Accepts() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		String userID=request.getParameter("rid");
	    int userId=0;
		if(userID!=null)
		{
			userId=Integer.parseInt(userID);
			session.setAttribute("userId", userId);
		}else{
			Integer a=(Integer)session.getAttribute("userId");
			if(a!=null)
				userId=a;			
		}
		if(userId==0){
			session.removeAttribute("userId");
			request.getRequestDispatcher("attend/error.jsp").forward(request, response);
			return;
		}
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
		String pagerMethod=request.getParameter("pagerMethod");
		if (pagerMethod != null) {
			startTime=(String)session.getAttribute("startTime");
			endTime=(String)session.getAttribute("endTime");
		}
		
		UserInfoDao userDao=new UserInfoDao();
		int totalRows=userDao.getAppCount(userId,startTime,endTime);
		Page page=PageHelp.getPager(request, totalRows);
		List<Appeal> applis=userDao.getAttendance(page.getPageSize(), page.getStartRow(), userId ,startTime,endTime);
		
		request.setAttribute("ui", userId);
		request.setAttribute("applis", applis);
		request.setAttribute("page", page);
		session.setAttribute("startTime",startTime);
		session.setAttribute("endTime",endTime);
		request.getRequestDispatcher("attend/admin/accept.jsp").forward(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
